import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import javax.swing.event.*;

public class FontChooser extends JFrame{
    private JPanel jp1,jp2,jp3,jp4,jp5,jp6;
    private JButton jbapply,jbpreview,jbcancel;
    private List listfont,listsize;
    private JLabel previewlabel;
    private JFrame frame;
    private String resultfont,resultsize;
    private String[] fontlist;
    private JCheckBox bold,italic;
    private Font result;
    private int lastsize;

    public FontChooser(final Component jt1,final Component jt2){
        frame = new JFrame("字体选择框");
        Container c = frame.getContentPane();
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        jp5 = new JPanel();
        jp6 = new JPanel();
        jbapply = new JButton("应用");
        jbpreview = new JButton("预览");
        jbcancel = new JButton("取消");
        listfont = new List(20);
        listsize = new List(20);
        previewlabel = new JLabel("preview",JLabel.CENTER);

        fontlist=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for(int i = 0;i<fontlist.length;i++)
            listfont.add(fontlist[i]);

        String[] num = {"8","9","10","11","12","13","14","15","16","17","18","19","19","20","21","22","23"
                ,"24","25","26","27","28","29","30"};
        for(int i = 0;i<num.length;i++)
            listsize.add(num[i]);

        jp1.setBorder(BorderFactory.createTitledBorder("字体"));
        jp1.add(listfont);

        jp2.setBorder(BorderFactory.createTitledBorder("大小"));
        jp2.add(listsize);

        jp3.setLayout(new GridLayout(2,1));
        jp3.setBorder(BorderFactory.createTitledBorder("样式"));
        jp3.add(bold = new JCheckBox("粗体",false));
        jp3.add(italic = new JCheckBox("斜体",false));

        jp4.setLayout(new FlowLayout());
        jp4.add(jp1);
        jp4.add(jp2);
        jp4.add(jp3);

        jp5.add(jbapply);
        jp5.add(jbpreview);
        jp5.add(jbcancel);

        jp6.setBorder(BorderFactory.createTitledBorder("预览"));
        jp6.setLayout(new BorderLayout());
        jp6.add(previewlabel,BorderLayout.SOUTH);

        c.setLayout(new BorderLayout());
        c.add(jp4,BorderLayout.NORTH);
        c.add(jp6,BorderLayout.CENTER);
        c.add(jp5,BorderLayout.SOUTH);

        jbapply.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                applyfont(jt1);
                applyfont(jt2);

                dispose();
                frame.setVisible(false);
            }
        });

        jbpreview.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                previewfont();
            }
        });

        jbcancel.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource() == jbcancel){
                    resultfont = null;
                    result = null;
                    lastsize = 0;

                    dispose();
                    frame.setVisible(false);
                    previewlabel.setFont(new Font("宋体",Font.PLAIN,15));
                }
            }
        });

        frame.pack();
        frame.setVisible(true);
    }
    private void previewfont(){
        resultfont = listfont.getSelectedItem();
        resultsize = listsize.getSelectedItem();
        lastsize = Integer.parseInt(resultsize);
        int style = Font.PLAIN;

        if(bold.isSelected())
            style = Font.BOLD;

        if(italic.isSelected())
            style += Font.ITALIC;

        result = new Font(resultfont,style,lastsize);

        previewlabel.setFont(result);
        pack();
    }

    private void applyfont(Component jt1){
        resultfont = listfont.getSelectedItem();
        resultsize = listsize.getSelectedItem();
        lastsize = Integer.parseInt(resultsize);
        int style = Font.PLAIN;

        if(bold.isSelected())
            style = Font.BOLD;

        if(italic.isSelected())
            style += Font.ITALIC;

        result = new Font(resultfont,style,lastsize);

        jt1.setFont(result);
    }

}

